package br.com.android.games.daedal;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Splash extends Activity {

	private volatile boolean active = true;
	private TextView touch;
	private ProgressBar progress;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		/**
		 * Desligar o titulo da janela
		 */
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		// setContentView(new MyRenderer(this));

		setContentView(R.layout.splash);

		touch = (TextView) findViewById(R.id.ttouch);
		progress = (ProgressBar) findViewById(R.id.progressBar);

		final int welcomeScreenDisplay = 1000; 

		touch.setText(R.string.app_loading);

		Thread welcomeThread = new Thread() {

			int wait = 0;
			boolean vcontrol = true;

			@Override
			public void run() {

				vcontrol = true;

				try {
					while (Splash.this.active) {
						if (wait >= welcomeScreenDisplay) {
							if (vcontrol)
								handler.sendEmptyMessage(TextView.VISIBLE);
							else
								handler.sendEmptyMessage(TextView.INVISIBLE);

							vcontrol = !vcontrol;
							wait = 0;
						}

						sleep(100);
						wait += 100;
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					finish();
					startActivity(new Intent(Splash.this,
							SelectScreen.class));
				}
			}
		};
		welcomeThread.start();// */
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == TextView.VISIBLE) {
				progress.setVisibility(ProgressBar.INVISIBLE);
				touch.setVisibility(TextView.VISIBLE);
				touch.setText(R.string.app_anykey);
			} else
				touch.setText("");
		}
	};

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			this.active = false;
		}
		return true;
	}
}
